Conditions | 26 |
Paths | 24 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like kirkiDependencies.evaluate often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | var kirkiDependencies = { |
||
85 | evaluate: function( value1, value2, operator ) { |
||
86 | var found = false, |
||
87 | result = null; |
||
88 | |||
89 | if ( '===' === operator ) { |
||
90 | result = value1 === value2; |
||
91 | } else if ( '==' === operator || '=' === operator || 'equals' === operator || 'equal' === operator ) { |
||
92 | result = value1 == value2; // jshint ignore:line |
||
93 | } else if ( '!==' === operator ) { |
||
94 | result = value1 !== value2; |
||
95 | } else if ( '!=' === operator || 'not equal' === operator ) { |
||
96 | result = value1 != value2; // jshint ignore:line |
||
97 | } else if ( '>=' === operator || 'greater or equal' === operator || 'equal or greater' === operator ) { |
||
98 | result = value2 >= value1; |
||
99 | } else if ( '<=' === operator || 'smaller or equal' === operator || 'equal or smaller' === operator ) { |
||
100 | result = value2 <= value1; |
||
101 | } else if ( '>' === operator || 'greater' === operator ) { |
||
102 | result = value2 > value1; |
||
103 | } else if ( '<' === operator || 'smaller' === operator ) { |
||
104 | result = value2 < value1; |
||
105 | } else if ( 'contains' === operator || 'in' === operator ) { |
||
106 | if ( _.isArray( value2 ) ) { |
||
107 | found = false; |
||
108 | _.each( value2, function( index, value ) { |
||
109 | if ( _.isNumber( value ) ) { |
||
110 | value = parseInt( value, 10 ); |
||
111 | } |
||
112 | if ( value1.indexOf( value ) > -1 ) { |
||
113 | found = true; |
||
114 | } |
||
115 | } ); |
||
116 | return found; |
||
117 | } else if ( _.isObject( value2 ) ) { |
||
118 | found = false; |
||
119 | if ( ! _.isUndefined( value2[ value1 ] ) ) { |
||
120 | found = true; |
||
121 | } |
||
122 | |||
123 | _.each( value2, function( subValue ) { |
||
124 | if ( value1 === subValue ) { |
||
125 | found = true; |
||
126 | } |
||
127 | } ); |
||
128 | return found; |
||
129 | } else if ( _.isString( value2 ) ) { |
||
130 | return value1.indexOf( value2 ) > -1; |
||
131 | } |
||
132 | } |
||
133 | if ( null === result ) { |
||
134 | return true; |
||
135 | } |
||
136 | return result; |
||
137 | } |
||
138 | }; |
||
143 |
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
let
orconst
. These variables/constants are only valid in the code block where they have been declared.Consider the following two pieces of code:
and
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.
To know more about this ECMA6 feature, look at the MDN pages on let and const.